package com.byd.sealmanage.web;

import com.alibaba.fastjson.JSONObject;
import com.byd.sealmanage.config.CommonResultOutput;
import com.byd.sealmanage.modal.bo.ProcessParamVO;
import com.byd.sealmanage.modal.po.TbUser;
import com.byd.sealmanage.service.ProcessManageService;
import com.byd.sealmanage.util.JsonUtil;
import com.byd.sealmanage.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/processManage")
public class ProcessManageController {

    @Autowired
    private ProcessManageService processManageService;

    /**
     * 获取我的申请页面数据
     * @param processParamVO
     * @return
     */
    @RequestMapping(value = "/getMyApplyData",method = RequestMethod.GET)
    public JSONObject getMyApplyData(ProcessParamVO processParamVO){
        TbUser loginUser = ShiroUtils.getUserEntity();
        processParamVO.setApplyNumber(loginUser.getJobnumber());
        return processManageService.getMyApplyDataService(processParamVO);
    }

    /**
     * 获取流程详情页信息
     * @return
     */
    @RequestMapping(value = "/getDetailBasicInfo",method = RequestMethod.GET)
    public JSONObject getDetailBasicInfo(@RequestParam(value = "trackId")String trackId){
        return processManageService.getDetailBasicInfo(trackId);
    }

    /**
     * 获取刻制申请流程相关的印章
     * @param trackId
     * @return
     */
    @RequestMapping(value = "/getApplySealInfo",method = RequestMethod.GET)
    public JSONObject getApplySealInfo(String trackId){
        return processManageService.getApplySealInfo(trackId);
    }

    /**
     * 获取流程节点信息
     * @param trackId
     * @return
     */
    @RequestMapping(value = "/getProcessNodeInfo",method = RequestMethod.GET)
    public JSONObject getProcessNodeInfo(String trackId){
        return processManageService.getProcessNodeInfo(trackId);
    }

    /**
     * 获取我的待办页面数据
     * @return
     */
    @RequestMapping(value = "/getMyNotDoData",method = RequestMethod.GET)
    public JSONObject getMyNotDoData(ProcessParamVO processParamVO){
        TbUser loginUser = ShiroUtils.getUserEntity();
        processParamVO.setApplyNumber(loginUser.getJobnumber());
        return processManageService.getMyNotDoTableData(processParamVO);
    }

    /**
     * 刻制申请同意流程
     * @return
     */
    @RequestMapping(value = "/agreeProcess",method = RequestMethod.POST)
    public JSONObject agreeProcess(@RequestBody Map<String,String> map){
        JSONObject jsonObject;
        try{
            TbUser loginUser = ShiroUtils.getUserEntity();
            jsonObject = processManageService.agreeProcessService(map,loginUser);
        }catch(Exception e){
            CommonResultOutput commonResultOutput = new CommonResultOutput(999,"执行出现异常");
            jsonObject = JsonUtil.toJSONObject(commonResultOutput);
        }
        return jsonObject;
    }

    /**
     * 刻制申请驳回流程
     * @param map
     * @return
     */
    @RequestMapping(value = "/rejectProcess",method = RequestMethod.POST)
    public JSONObject rejectProcess(@RequestBody Map<String,String> map){
        JSONObject jsonObject;
        try{
            TbUser loginUser = ShiroUtils.getUserEntity();
            jsonObject = processManageService.rejectProcessService(map,loginUser);
        }catch(Exception e){
            CommonResultOutput commonResultOutput = new CommonResultOutput(999,"执行出现异常");
            jsonObject = JsonUtil.toJSONObject(commonResultOutput);
        }
        return jsonObject;
    }

    /**
     * 驳回后拟稿人重新提交流程
     * @return
     */
    @RequestMapping(value = "/submitProcessAgain",method = RequestMethod.POST)
    public JSONObject submitProcessAgain(@RequestBody Map<String,String> map){
        JSONObject jsonObject;
        try{
            TbUser loginUser = ShiroUtils.getUserEntity();
            jsonObject = processManageService.submitAgainService(map,loginUser);
        }catch(Exception e){
            CommonResultOutput commonResultOutput = new CommonResultOutput(999,"执行出现异常");
            jsonObject = JsonUtil.toJSONObject(commonResultOutput);
        }
        return jsonObject;
    }

    /**
     * 流程转交
     * @return
     */
    @RequestMapping(value = "/changeApprovePeople",method = RequestMethod.POST)
    public JSONObject changeApprovePeople(@RequestBody Map<String,String> map){
        JSONObject jsonObject;
        try{
            TbUser loginUser = ShiroUtils.getUserEntity();
            jsonObject = processManageService.changeApprovePeopleService(map,loginUser);
        }catch(Exception e){
            CommonResultOutput commonResultOutput = new CommonResultOutput(999,"执行出现异常");
            jsonObject = JsonUtil.toJSONObject(commonResultOutput);
        }
        return jsonObject;
    }

    /**
     * 流程撤回
     * @return
     */
    @RequestMapping(value = "/backProcess",method = RequestMethod.POST)
    public JSONObject backProcess(@RequestBody Map<String,String> map){
        JSONObject jsonObject;
        try{
            jsonObject = processManageService.backProcessService(map);
        }catch(Exception e){
            CommonResultOutput commonResultOutput = new CommonResultOutput(999,"执行出现异常");
            jsonObject = JsonUtil.toJSONObject(commonResultOutput);
        }
        return jsonObject;
    }

    /**
     * 流程废止
     * @param map
     * @return
     */
    @RequestMapping(value = "/stopProcess",method = RequestMethod.POST)
    public JSONObject stopProcess(@RequestBody Map<String,String> map){
        JSONObject jsonObject;
        try{
            TbUser loginUser = ShiroUtils.getUserEntity();
            jsonObject = processManageService.stopProcessService(map,loginUser);
        }catch(Exception e){
            CommonResultOutput commonResultOutput = new CommonResultOutput(999,"执行出现异常");
            jsonObject = JsonUtil.toJSONObject(commonResultOutput);
        }
        return jsonObject;
    }

    /**
     * 获取我的审批页面数据
     * @return
     */
    @RequestMapping(value = "/myApprovedPageData",method = RequestMethod.GET)
    public JSONObject myApprovedPageData(ProcessParamVO processParamVO){
        TbUser loginUser = ShiroUtils.getUserEntity();
        processParamVO.setApprovePeople(loginUser.getJobnumber());
        return processManageService.getMyApprovedPageData(processParamVO);
    }

}
